def bound_range_str(r):
    a=r[0]
    b=r[-1]
    if a in "[]": r=r[1:]
    if b in "[]":r=r[:-1]
    
    t=[i.strip() for i in r.split(':')]
    pn=0
    tmp=[len(i.split('.')[1]) for i in t if '.' in i]
    if tmp:
        pn=max(tmp)
    pf=10**-pn
    pi=10**pn
    if len(t)==1:
        return t
    t=[int(float(i)*pi) for i in t]
    if len(t)==2:
        t.append(1)
        t[-1]*=pi
    if a=="]":t[0]+=t[-1]
    if b=="]":t[1]+=t[-1]
    if pn==0:
        return [str(int(round(i*pf))) for i in range(*t)]
    else:
        return [str(round(i*pf,pn)) for i in range(*t)]

def getLength(s):
    if s[0]=='"':s=s[1:]
    if s[-1]=='"':s=s[:-1]
    if s[0]=='{':s=s[1:]
    if s[-1]=='}':s=s[:-1]
    return len(s.split(','))
    
def parse_par(file):
    s=open(file).read()
    l=[i.strip() for i in s.strip().split('\n')]
    l=[i for i in l if i and i[0] not in "#%"]
    d={}
    k=[]
    for i in l:
        t=[j.strip() for j in i.split('=')]
        lt=len(t)
        if lt<2:continue
        if lt==2:
            d[t[0]]=t[1]
            k.append(t[0])
        else:
            d[t[0]]="=".join(t[1:])
            k.append(t[0])
        
    return k,d

def prepare(k,d):
    pl=[]
    for i in k:
        if d[i][0] in "[]":
            d[i]=bound_range_str(d[i])
            pl.append(i)
        elif d[i][0] =="{":
            d[i]=d[i][1:-1].split(',')
            pl.append(i)
    return pl
def __permute_inc(l,p):
    n=len(p)-1
    while n>=0:
        p[n]+=1
        if p[n]==l[n]:
            p[n]=0
            n-=1
        else:
            return 0
    return 1
def permute(k,d,n):
    l=[]
    l2=[]
    r={}
    for i in k:
        if type(d[i])==list:
            l.append(len(d[i]))
            l2.append(i)
#            l.insert(0,len(d[i]))
#            l2.insert(0,i)
        else:
            r[i]=d[i]
    ln=len(l)
    p=[0]*ln
    for i in range(n):
        n+=1
        __permute_inc(l,p)
    m=0
    for i in l2:
        r[i]=d[i][p[m]]
#        print i,"m=",m,"p[m]=",p[m],d[i][p[m]]
        m+=1
    yield (n,r)
    while __permute_inc(l,p)==0:
        m=0
        n+=1
        for i in l2:
            r[i]=d[i][p[m]]
            m+=1
        yield (n,r)

def cal_nit(d):
#   calculates number of iterations
    s=1
    for i in d:
        if type(d[i])==list:s*=len(d[i])
    return s

            